package com.net.xpay.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.msg.AlipayMsgClient;
import com.net.xpay.core.CoreProperties;
import com.net.xpay.core.helper.EnvironmentHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;

import java.io.IOException;

@Slf4j
@Configuration
public class CoreConfig {

    @Autowired
    private AlipayProperties alipayProperties;

    @Autowired
    private EnvironmentHelper environmentHelper;

    @Autowired
    private ResourceLoader resourceLoader;

    @Autowired
    private CoreProperties coreProperties;

    @Bean
    public PoseidonAlipayClient xpayAlipayClient() throws AlipayApiException {
        if (coreProperties.getDisableAlipayClient() != null && coreProperties.getDisableAlipayClient() == 1) {
            return new PoseidonAlipayClient(null, -1);
        }
        String url = alipayProperties.getAlipayTopUrl();
        String appId = alipayProperties.getAlipayAppId();
        String appPrivateKey = alipayProperties.getAppPrivateKey();
        String rootCertPath = alipayProperties.getRootCertPath();
        String certPath = alipayProperties.getCertPath();
        String alipayPublicCertPath = alipayProperties.getAlipayPublicCertPath();

        //本地开发环境 证书路径需要读取本地路径
        if (environmentHelper.isDev()) {
            String dirname = null;
            try {
                dirname = resourceLoader.getResource("classpath:cert/alipay").getURL().getFile();
            } catch (IOException e) {
                log.error("get cert path error", e);
                throw new RuntimeException("get cert path error");
            }
            rootCertPath = dirname + "/alipayRootCert.crt";
            certPath = dirname + "/appCertPublicKey_" + alipayProperties.getAlipayAppId() + ".crt";
            alipayPublicCertPath = dirname + "/alipayCertPublicKey_RSA2.crt";
        }

        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        certAlipayRequest.setServerUrl(url);
        certAlipayRequest.setAppId(appId);
        certAlipayRequest.setPrivateKey(appPrivateKey);
        certAlipayRequest.setFormat("json");
        certAlipayRequest.setCharset("GBK");
        certAlipayRequest.setSignType("RSA2");
        certAlipayRequest.setCertPath(certPath);
        certAlipayRequest.setAlipayPublicCertPath(alipayPublicCertPath);
        certAlipayRequest.setRootCertPath(rootCertPath);
        DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);

        return new PoseidonAlipayClient(alipayClient, -1);
    }

    @Bean("xpayAlipayMsgClient")
    public AlipayMsgClient xpayAlipayMsgClient() throws Exception {
        if (coreProperties.getDisableAlipayClient() != null && coreProperties.getDisableAlipayClient() == 1) {
            return AlipayMsgClient.getInstance("");
        }

        String serverHost = alipayProperties.getServerHost();
        String appId = alipayProperties.getAlipayAppId();
        String appPrivateKey = alipayProperties.getAppPrivateKey();
        String rootCertPath = alipayProperties.getRootCertPath();
        String certPath = alipayProperties.getCertPath();
        String alipayPublicCertPath = alipayProperties.getAlipayPublicCertPath();

        //本地开发环境 证书路径需要读取本地路径
        if (environmentHelper.isDev()) {
            String dirname = null;
            try {
                dirname = resourceLoader.getResource("classpath:cert/alipay").getURL().getFile();
            } catch (IOException e) {
                log.error("get cert path error", e);
                throw new RuntimeException("get cert path error");
            }
            rootCertPath = dirname + "/alipayRootCert.crt";
            certPath = dirname + "/appCertPublicKey_" + alipayProperties.getAlipayAppId() + ".crt";
            alipayPublicCertPath = dirname + "/alipayCertPublicKey_RSA2.crt";
        }

        AlipayMsgClient alipayMsgClient = AlipayMsgClient.getInstance(appId);

        alipayMsgClient.setConnector(serverHost);
        alipayMsgClient.setSecurityCertConfig("RSA2", appPrivateKey, certPath, alipayPublicCertPath, rootCertPath);
        alipayMsgClient.setCharset("UTF-8");

        return alipayMsgClient;
    }
}
